<?php
/**
 *	This file is part of rtfdoc.
 *
 *	rtfdoc is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	rtfdoc is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with rtfdoc.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
class RTFDocumentHeaderView
{
    /**
     * @var RTFDocument
     */
    public $model;
    /**
     * holds the rtf-text
     *
     * @var string
     */
    private $rtf_text;
    /**
     * parses input and returns rtf-text
     *
     * @param RTFDocument $model
     * @return string
     */
    public function parse(RTFDocument $model)
    {
        $this->model = $model;
        $this->rtf_text = $this->buildHeader();
        return $this->rtf_text;
    }
    /**
     * returns the rtf-version
     *
     * @return string
     */
    public function getRTFVersionString()
    {
        return '\rtf1';
    }
    /**
     * returns the character set to use
     *
     * @return string
     */
    public function getCharset()
    {
        return '\ansi';
    }
    /**
     * constructs the header
     *
     * @return string
     */
    private function buildHeader()
    {
        //  \rtf <charset> \deff? <fonttbl>  <filetbl>? <colortbl>? <stylesheet>? <listtables>? <revtbl>?
        $header_string = $this->getRTFVersionString();
        $header_string.= $this->getCharset();
        $header_string.= '\deff0';
        $header_string.= $this->getFontTableDefinition();
        $header_string.= $this->getFileTableDefinition();
        $header_string.= $this->getColorTableDefinition();
        $header_string.= $this->getStyleSheetDefinition();
        $header_string.= $this->getListTableDefinition();
        $header_string.= $this->getRevisionTableDefinition();
        return $header_string;
    }
    /**
     * gets rtf for fonttable
     *
     * @return string
     */
    private function getFontTableDefinition()
    {
        $main_def = '{\fonttbl%s}';
        $font_string = '';
        $counter = 0;
        if (count($this->model->getFonts()) > 0) {
            foreach($this->model->getFonts() as $font) {
                $font_string.= sprintf('{\f%d\f%s %s;}', $counter, $font->getType(), $font->getFace());
            }
        }
        return sprintf($main_def, $font_string);
    }
    /**
     * gets rtf for filetable
     *
     * @return string
     */
    private function getFileTableDefinition()
    {
        return "";
    }
    /**
     * gets rtf for colortable
     *
     * @return string
     */
    private function getColorTableDefinition()
    {
        $main_def = '{\colortbl;%s}';
        $color_string = '';
        if (count($this->model->getColors()) > 0) {
            foreach($this->model->getColors() as $color) {
                list($red, $green, $blue) = $color->getRGB();
                $color_string.= sprintf('\red%d\green%d\blue\%d;', $red, $green, $blue);
            }
        }
        return sprintf($main_def, $color_string);
    }
    /**
     * gets rtf for stylesheet definition
     *
     * @return string
     */
    private function getStyleSheetDefinition()
    {
        return "";
    }
    /**
     * gets rtf for listtable definition
     *
     * @return string
     */
    private function getListTableDefinition()
    {
        return "";
    }
    /**
     * gets rtf for revisiontable definition
     *
     * @return string
     */
    private function getRevisionTableDefinition()
    {
        return "";
    }
}
?>